/*常用方法封装*/
const md5 = require('md5');
const configs = require('./config');
function crypto (str) {
  console.log(configs.md5pre+str);
  return md5(str);
}
function jiami (str) {
  console.log(configs.md5pre+str);
  return md5(md5(configs.md5pre)+md5(str));
}
function num_to_time (num) {
	var nowtime_num = Date.parse(new Date())/1000;
	var timestamp = num ? num : nowtime_num;
	var   now = new Date(timestamp*1000);
	var   year=now.getFullYear();
    var   month=now.getMonth()+1;
    if(month<10){
    	month = '0'+month;
    }
    var   day=now.getDate();
    if(day<10){
    	day = '0'+day;
    }
    var   hour=now.getHours();
    if(hour<10){
    	hour = '0'+hour;
    }
    var   minute=now.getMinutes();
    if(minute<10){
    	minute = '0'+minute;
    }
    var   second=now.getSeconds();
    if(second<10){
    	second = '0'+second;
    }
  	return   year+"-"+month+"-"+day+" "+hour+":"+minute;
  	//+":"+second  秒

}
function removeHtmlTab(tab) {
 return tab.replace(/<[^<>]+?>/g,'');//删除所有HTML标签
}

function escape2Html(str) {
 var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
 return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
}
//解码
function html_decode(str)
{
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/>/g, "&");
    s = s.replace(/</g, "<");
    s = s.replace(/>/g, ">");
    s = s.replace(/ /g, " ");
    s = s.replace(/'/g, "\'");
    s = s.replace(/"/g, "\"");
    s = s.replace(/<br>/g, "\n");
    return s;
}

function html_encode(str)
{
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, ">");
    s = s.replace(/</g, "<");
    s = s.replace(/>/g, ">");
    s = s.replace(/ /g, " ");
    s = s.replace(/\'/g, "'");
    s = s.replace(/\"/g, '"');
    s = s.replace(/\n/g, "<br>");
    return s;
}

function htmlDecodeByRegExp (str){
          var s = "";
          if(str.length == 0) return "";
          s = str.replace(/&amp;/g,"&");
          s = s.replace(/&lt;/g,"<");
          s = s.replace(/&gt;/g,">");
          s = s.replace(/&nbsp;/g," ");
          s = s.replace(/&#39;/g,"\'");
          s = s.replace(/&quot;/g,"\"");
          return s;
    }


module.exports = {
  md5:crypto,
  jiami:jiami,
  num2time:num_to_time,
  removeHtml:removeHtmlTab,
  str2html:escape2Html,
  gethtml:htmlDecodeByRegExp,
}
